package xyz.syyrjx.blog.hook;

import com.alibaba.dashscope.utils.Constants;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.transport.ProxyProvider;
import xyz.syyrjx.blog.dao.ai.AiIdentityDao;
import xyz.syyrjx.blog.dao.ai.AiIdentityGradeDao;
import xyz.syyrjx.blog.util.ConstValuePool;
import xyz.syyrjx.blog.util.ai.AiIdentityFlagUtil;
import xyz.syyrjx.blog.util.ai.AiUseCheckUtil;
import xyz.syyrjx.blog.util.ai.QwenModelService;
import xyz.syyrjx.blog.util.site.BaiduSubmitUtil;
import xyz.syyrjx.blog.util.site.BingSubmitUtil;
import xyz.syyrjx.blog.util.site.SiteMapUtil;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2024/2/4 15:26
 * @description ...
 * @since v0.0.1
 */
@Component
public class ApiCodeLoadAware implements EnvironmentAware, ApplicationContextAware {

    Environment environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;

        // 通义千问环境设置
        Constants.apiKey = environment.getProperty("ali.tqwen.key");

        // 百度资源提交
        BaiduSubmitUtil.endpoint = environment.getProperty("baidu.api.summit.link");
        BingSubmitUtil.endpoint = environment.getProperty("bing.api.summit.link");
        ConstValuePool.BLOG_HOST = environment.getProperty("blog.host");

        SiteMapUtil.siteMapPath = environment.getProperty("sitemap.path");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // 通义千问
        QwenModelService qwenModelService = applicationContext.getBean(QwenModelService.class);
        qwenModelService.createGen();


        // chatgpt、gpt4
        for (int i = 0; i < ConstValuePool.MAX_NUMBER_OF_PROXY_OPENAI_CLIENT_FREE; i++) {
            HttpClient httpClient = HttpClient.create().proxy(clientProxy ->
                    clientProxy.type(ProxyProvider.Proxy.HTTP) // 设置代理类型
                            .host("127.0.0.1") // 代理主机
                            .port(7890)); // 代理端口
            ConstValuePool.PROXY_OPENAI_CLIENT_FREE.add(WebClient.builder()
                    .clientConnector(new ReactorClientHttpConnector(httpClient))
                    .baseUrl("https://api.openai.com")
                    .defaultHeader("Authorization", "Bearer " + environment.getProperty("openai.key"))
                    .build());
        }

        // ai身份dao对象设置
        AiIdentityDao aiIdentityDao = applicationContext.getBean(AiIdentityDao.class);
        AiIdentityGradeDao aiIdentityGradeDao = applicationContext.getBean(AiIdentityGradeDao.class);
        AiIdentityFlagUtil.aiIdentityDao = aiIdentityDao;
        AiUseCheckUtil.aiIdentityDao = aiIdentityDao;
        AiUseCheckUtil.aiIdentityGradeDao = aiIdentityGradeDao;
    }
}
